/**
 * 公共普通列表混入js，简化文件的代码长度
 * 实例请看system/config的form.vue
 */
export default {
  data() {
    return {
      moduleName: '', //模块名称
      loading: true,
      total: 0,
      listData: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: undefined,
        orderBySort: undefined,
        where: {},
        like: {},
        find: {},
        time:{},
        date: {},
        datetime:{}
      },
      selections: [], // 选中列表

      exporting: false, // 导出状态




      idField: 'id', // 主键对应字段
      nameField: 'name', // 名称对应的字段


      isExpandAll: true,  // 是否展开，默认全部展开
      refreshTable: true,  // 重新渲染表格状态

      fullPath: '', // 当前路由的全路径

      //添加修改弹窗
      formShow: false,
      formData: null

      // 控制list-page布局，用到的时候自己添加
      // showSearch: true,
      // searchIcon: true,
      // showActions: true,
      // showToolbar: true,
      // showPage: true,
    }
  },
  methods: {
    // @overwrite 获取列表接口
    getList() {},

    // 默认搜索方法，可根据需要自行 @overwrite
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },

    // 重置搜索，使用list-search 根据resetRefresh会自动调用search回调，一般不需要
    reset() {},

    // 添加表单
    handleAdd() {
      this.formData = null
      this.formShow = true
    },

    // 编辑表单
    handleEdit(row) {
	  if (row instanceof Event) row = this.selections
      if (!row || typeof row !== 'object') return this.$modal.msgWarning('选择数据错误')
      if (Array.isArray(row)) row = row[0]
      let formId = this.idField in row ? row[this.idField] : ''
      if (!formId) return this.$modal.msgWarning('参数缺失')
      this.formData = { id: formId }
      this.formShow = true
    },

    // 表格选中数据
    handleSelection(selection) {
      this.selections = []
      if (!selection || typeof selection !== 'object') return
      if (Array.isArray(selection)) { // 多选
        this.selections = selection
      } else { // 单选
        this.selections = [selection]
      }
    },

    // 字段排序
    handleSort(row) {
      if (row.column.sortable !== 'custom') return
      const {prop, order} = row
      if (order && prop) {
        this.searchForm.orderByColumn = prop
        this.searchForm.orderBySort = order
      } else {
        this.searchForm.orderByColumn = ''
        this.searchForm.orderBySort = ''
      }
      this.search()
    },

    // @overwrite 调用删除接口 参数为 {id:'xxx,xx'}，
    // 若不满足可以重写 handleDelete
    toDelete(param){},
    // 删除按钮处理
    handleDelete(row, extend = {}) {
      let rows = []
      if (row instanceof Event) {
        rows = this.selections
      } else {
        if (!row || typeof row !== 'object') return this.$modal.msgWarning('选择数据错误')
        if (!Array.isArray(row)) rows = [row]
      }

      let list = []
      rows.map(item => {
        if (typeof item === 'object') {
          this.idField in item && list.push(item)
        } else if (typeof item === 'string' || typeof item === 'number') {
          list.push({[this.idField]: item})
        }
      })
      if (!list.length) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm(this.deleteTitle(rows)).then(() => {
        let ids = rows.map(item => item[this.idField])
        return this.toDelete({id: ids.join(','), ...extend})
      }).then((res) => {
        this.getList()
        this.$modal.msgSuccess(res?.msg || '删除成功')
      }).catch(() => {})
    },
    // 获取删除确认提示语
    deleteTitle(rows){
      let ids = rows.map(item => item[this.idField])
      let title = '确认 "删除" ' + this.moduleName + ' 编号为 "' + ids.join(',') + '" 的数据项？'
      if (rows.length === 1 && this.nameField && this.nameField in rows[0]) {
        title = '确认 "删除" ' + this.moduleName + ' "' + rows[0][this.nameField] + '" ？'
      }
      return title
    },

    // 展开折叠操作
    handleExpand() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      });
    },

    // 关闭tab
    closeTab() {
      this.$tab.closePage()
    },

    // 关闭当前打开指定tab
    closeOpenPage(obj) {
      this.$tab.closeOpenPage(obj)
    },

    // @overwrite 导出
    handleExport() {},
    // @overwrite 导出
    handleCache() {},
  }
}
